/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PCrearmapa.java
 *
 * Created on 20-sep-2011, 23:15:59
 */
package View.Almacen.BloquearUbicacion;
import javax.swing.*;
import java.awt.*;


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;



/**
 *
 * @author Michael
 */
public class PCargarMapa extends JPanel implements MouseListener{
    
    JButton botones[][];
    JButton boton;
    FBloquearUbicacion padre;
    public PCargarMapa(FBloquearUbicacion f) {
        padre=f;
        leerMapa();
        llenarMapa(padre.AnchoMapa,padre.AltoMapa);
        
    }
    public void leerMapa(){
        BufferedReader fin=null;
        try{
                fin = new BufferedReader(new FileReader("./mapa.txt"));
        }catch(IOException e){
            javax.swing.JOptionPane jOP = new javax.swing.JOptionPane(); 
            jOP.showMessageDialog(null, "Error de lectura","", 0);
        }
        
        String strAux[]= new String [1000];
        int i=0;
        while(true){
            try{
                strAux[i] = fin.readLine();
            }catch(Exception e){System.out.print("Error en leer\n");}   
            if (strAux[i]==null) break;
            i++;
        }
        padre.AltoMapa=i;
        padre.AnchoMapa=strAux[0].length();
        padre.MMapa= new int[padre.AltoMapa][padre.AnchoMapa];
        for (int j=0;j<padre.AltoMapa;j++){
            for (int k=0;k<padre.AnchoMapa;k++){
                padre.MMapa[j][k]=strAux[j].charAt(k)-48;
            }
        }        
    }
    public void llenarMapa(int ancho,int alto){
        this.removeAll();
        setLayout(null);
        setLayout(new GridLayout(alto,ancho));
        //setSize(500,500);
        botones=new JButton[alto][ancho];
        for (int i=0;i<alto;i++){
                for(int j=0;j<ancho;j++){
                    boton= new JButton(" ");//+(i+1)+"-"+(j+1));
                    boton.setSize(20,20);     
                    if (padre.MMapa[i][j]==0)
                        boton.setBackground(Color.yellow);
                    else
                        boton.setBackground(Color.BLACK);                    
                    
                    boton.setFocusPainted(false);
                    botones[i][j]=boton;                
                    boton.addMouseListener(new MouseAdapter(){
                    public void mousePressed(MouseEvent e) {
                            if (((JButton)e.getSource()).getBackground()==Color.ORANGE){                                 
                                ((JButton)e.getSource()).setBackground(Color.YELLOW);
                            }
                            else if (((JButton)e.getSource()).getBackground()==Color.YELLOW){                                 
                                ((JButton)e.getSource()).setBackground(Color.ORANGE);
                            }
                    }});
                }
        }        
        for (int i=0;i<alto;i++){
                for(int j=0;j<ancho;j++){
                     add(botones[i][j]);
                }
        }
        
    }
    @Override
    public void mouseClicked(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mousePressed(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    

}
